package com.goldgov.interfacemanage.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.goldgov.conference.service.Conference;
import com.goldgov.conference.service.ConferenceQuery;
import com.goldgov.conference.service.ConferenceUser;
import com.goldgov.distancelearning.service.Distancelearning;
import com.goldgov.interfacemanage.service.InterfaceManage;
import com.goldgov.interfacemanage.service.InterfaceManageQuery;
import com.goldgov.interfacemanage.service.InterfaceManageService;
import com.goldgov.interfacemanage.service.InterfaceParam;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.securityworkbench.attachment.AttachementBean;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.JsonUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: InterfaceManageController 
 * @Description: 接口管理
 * @author hanxiangyu
 * @date 2018年7月23日 下午4:55:48 
 */
@RestController
@RequestMapping("/workbench/interfacemanage")
@Api("接口关联")
public class InterfaceManageController {
	
	@Autowired
	public InterfaceManageService interfaceManageService;
	
	@GetMapping("/preAdd")
	@ApiOperation(value = "预新增", notes ="预新增")
	public JsonSuccessObject<String> preAdd(HttpServletRequest request){
		String id = interfaceManageService.preAdd();
		JsonSuccessObject<String> returnResult = new JsonSuccessObject<String>(id);
		return returnResult;
	}
	
	
	@RequestMapping("/listInterfaceManage")
	@ApiOperation(value = "查询接口记录", notes ="查询接口记录")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query"),
		@ApiImplicitParam(name = "searchInterfaceName", value = "接口名称", paramType = "query"),
		@ApiImplicitParam(name = "searchInterfaceType", value = "接口类型", paramType = "query") })
	public JsonQueryObject<InterfaceManage> getInterfaceManageList(@ApiIgnore InterfaceManageQuery query,HttpServletRequest request,Model model){
		//是否包含下级单位
		List<InterfaceManage> list = interfaceManageService.getInterfaceManageList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	} 
	
	
	@PostMapping
	@ApiOperation(value = "新增接口记录", notes = "新增接口记录" )
	public JsonObject<Object> addInterfaceManage(InterfaceManage obj,HttpServletRequest request
			){
		SwbAuthUser u = LoginAuthUser.getUserByAdmin(request);
		obj.setCreateUserId(u.getEntityID());//创建人id
		obj.setCreateUserName(u.getUserName());//创建人姓名
		obj.setActiveStates(InterfaceManageQuery.ACTIVE_STATES_YES);
		obj.setCreateDate(new Date());
		if (interfaceManageService.addInterfaceManage(obj)) {
			return JsonSuccessObject.SUCCESS;
		}else {
			return JsonUtil.error("新增失败");
		}
	} 
	@PostMapping("addParams")
	@ApiOperation(value = "新增接口记录参数", notes = "新增接口记录参数" )
	public JsonObject<Object> addParams(InterfaceParam obj,HttpServletRequest request){
		if (interfaceManageService.addInterfaceParam(obj)) {
			return JsonSuccessObject.SUCCESS;
		}else {
			return JsonUtil.error("新增失败");
		}
	} 
	@PostMapping("/updateInterface")
	@ApiOperation(value = "修改接口记录", notes = "修改接口记录" )
	public JsonObject<Object> updateInterfaceManage(InterfaceManage obj,HttpServletRequest request){
		SwbAuthUser u = LoginAuthUser.getUserByAdmin(request);
		obj.setUpdateUserId(u.getEntityID());//创建人id
		obj.setUpdateUserName(u.getUserName());//创建人姓名
		if (interfaceManageService.updateInterfaceManage(obj)) {
			return JsonSuccessObject.SUCCESS;
		}else {
			return JsonUtil.error("新增失败");
		}
	} 
	
	@DeleteMapping
	@ApiOperation(value = "删除接口记录", notes = "删除接口记录" )
	public JsonObject<Object> deleteInterfaceManage(@ApiParam(value = "interfaceID", required = true) @RequestParam("ids") String[] ids){
		if (interfaceManageService.deleteInterfaceManage(ids)) {
			return JsonSuccessObject.SUCCESS;
		}else {
			return JsonUtil.error("新增失败");
		}
	} 
	
	@GetMapping("/{id}")
	@ApiOperation(value = "根据主键查询会议记录", notes ="工作日志详情")
	public JsonSuccessObject<InterfaceManage> getInterfaceById(@ApiParam(value="interfaceID",required=false) @PathVariable("id") String id,HttpServletRequest request){
		InterfaceManage interfaceManage = interfaceManageService.getInterfaceById(id);
		JsonSuccessObject<InterfaceManage> returnResult = new JsonSuccessObject<>(interfaceManage);
		return returnResult;
	}
	
	
}
